package com.bl.student.api.controller.pay;

import com.bl.student.api.controller.BaseController;
import com.bl.student.common.ApiResponseBody;
import com.bl.student.common.enums.pay.WechatPayPayInfoVo;
import com.bl.student.common.utils.JSONUtil;
import com.bl.student.service.biz.pay.PayService;
import com.bl.student.service.vo.pay.AlipayPayInfoVo;
import com.bl.student.service.vo.pay.AlipaySyncResultRequest;
import com.bl.student.service.vo.pay.PayListVo;
import com.bl.student.service.vo.pay.WechatPaySyncResultRequest;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * Created by yujingyi on 2017/9/23.
 */
@RequestMapping(value = {"/student/apis/pay"})
@Api(value = "支付相关api", description = "支付相关api")
@Log4j2
@RestController
public class PayController extends BaseController {

    @Autowired
    PayService payService;

    @ApiOperation(value = "获取活动微信支付信息", notes = "")
    @RequestMapping(value = {"/vip_wxpay_pay_info"}, method = {GET})
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public ApiResponseBody activityWechatPayPayInfo(
            @ApiParam(required = true, value = "活动id")
            @RequestParam(value = "applyId", required = true) String applyId,
            HttpServletRequest request) {

        WechatPayPayInfoVo wechatPayPayInfoVo = payService.getWechatPayAppPayInfo(getUserInfo(request), applyId);

        return ApiResponseBody.createSuccessBody(wechatPayPayInfoVo);
    }

    @ApiOperation(value = "微信同步返回结果处理接口", notes = "")
    @RequestMapping(value = {"/wxpay_sync_result"}, method = {POST})
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public ApiResponseBody wechatPaySyncResult(
            @Valid @RequestBody WechatPaySyncResultRequest body,
            BindingResult result,
            HttpServletRequest request) {

        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }

        payService.updateWechatPayAppPayInfoSync(body, getUserInfo(request));

        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "支付记录", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", required = false, paramType = "query", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每List页大小", required = false, paramType = "query", dataType = "int", defaultValue = "10"),
            @ApiImplicitParam(name = "type", value = "PARTYA,PARTYB", required = false, paramType = "query", dataType = "String", defaultValue = "PARTYA")})
    @GetMapping(value = {"/get_list"})
    public ApiResponseBody<PayListVo> payList(
            @RequestParam(value = "page", required = false, defaultValue = "1") int page,
            @RequestParam(value = "size", required = false, defaultValue = "10") int size,
            @RequestParam(value = "type", required = false, defaultValue = "10") String type,
            HttpServletRequest request) {

        Pageable pageable = new PageRequest(page - 1, size);

        PayListVo  payListVo = payService.getPayAndApplyList(getUserInfo(request).getId(),pageable, type);

        return ApiResponseBody.createSuccessBody(payListVo);
    }

}
